package com.oristand.printer.address.view;

import com.oristand.printer.address.MainApp;
import com.oristand.printer.address.model.LabelDetail;
import com.oristand.printer.address.util.CommonMethodUtil;
import com.oristand.printer.address.util.Constants;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;

import java.util.stream.Collectors;

/**
 * 打印记录对应label
 *
 * @author chenkailiang
 * @date 2020/9/7
 */
public class PrintRecordDetailController {

    private Stage dialogStage;

    @FXML
    private Label fileNameLabel;
    @FXML
    private Label printDateLabel;
    @FXML
    private TableView<LabelDetail> labelDetailTable;
    @FXML
    private TableColumn<LabelDetail, String> indexColumn;
    @FXML
    private TableColumn<LabelDetail, String> orderIdColumn;
    @FXML
    private TableColumn<LabelDetail, String> itemCodeColumn;
    @FXML
    private TableColumn<LabelDetail, String> trackingNumberColumn;
    @FXML
    private Label totalCountLabel;
    @FXML
    private Button backButtonTop;
    @FXML
    private Button backButtonBottom;
    @FXML
    private TextField trackingNumberTextField;
    @FXML
    private TextField orderIdTextField;
    @FXML
    private TextField itemCodeTextField;
    @FXML
    private Button searchLabelButton;

    private ObservableList<LabelDetail> labelDetailList = FXCollections.observableArrayList();


    /**
     * 返回
     */
    @FXML
    public void doBack(){
        dialogStage.close();
    }

    /**
     * 初始化
     */
    @FXML
    private void initialize() {
        backButtonTop.setVisible(true);
        backButtonBottom.setVisible(true);
        searchLabelButton.setVisible(true);
        labelDetailTable.setPlaceholder(new Text("No Data"));
        if(labelDetailTable.getItems()!=null && labelDetailTable.getItems().size()>0){
            orderIdColumn.setCellValueFactory(cellData -> cellData.getValue().orderIdProperty());
            itemCodeColumn.setCellValueFactory(cellData -> cellData.getValue().itemCodeProperty());
            trackingNumberColumn.setCellValueFactory(cellData -> cellData.getValue().trackingNumberProperty());
            indexColumn.setCellValueFactory(cellData -> cellData.getValue().indexProperty());
        }
        labelDetailTable.getSelectionModel().setCellSelectionEnabled(true);
        labelDetailTable.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        labelDetailTable.setOnKeyPressed(event -> {
            if (Constants.KEY_CODE_COMBINATION.match(event)) {
                CommonMethodUtil.copySelectionToClipboard(labelDetailTable);
            }
        });
    }

    /**
     * 查找label
     */
    @FXML
    private void handleLabelSearch(){
        String trackingNumber = trackingNumberTextField.getText().trim();
        String orderId = orderIdTextField.getText().trim();
        String itemCode = itemCodeTextField.getText().trim();
        ObservableList<LabelDetail> filterList = labelDetailList;
        ObservableList<LabelDetail> temp ;
        if(StringUtils.isNotBlank(trackingNumber)){
            temp = FXCollections.observableArrayList();
            temp.addAll(filterList.stream().filter(labelTemp -> trackingNumber.equalsIgnoreCase(labelTemp.getTrackingNumber())).collect(Collectors.toList()));
            filterList = temp;
        }
        if(StringUtils.isNotBlank(orderId)){
            temp = FXCollections.observableArrayList();
            temp.addAll(filterList.stream().filter(labelTemp -> orderId.equalsIgnoreCase(labelTemp.getOrderId())).collect(Collectors.toList()));
            filterList = temp;
        }
        if(StringUtils.isNotBlank(itemCode)){
            temp = FXCollections.observableArrayList();
            temp.addAll(filterList.stream().filter(labelTemp -> itemCode.equalsIgnoreCase(labelTemp.getItemCode())).collect(Collectors.toList()));
            filterList = temp;
        }
        labelDetailTable.setItems(filterList);
        initialize();
    }

    /**
     * 赋值
     * @param mainApp mainApp
     */
    public void setMainApp(MainApp mainApp) {
        labelDetailTable.setItems(mainApp.getSelectPrintRecordLabelList());
        fileNameLabel.setText(mainApp.getSelectedPrintRecord().getFileName());
        printDateLabel.setText(mainApp.getSelectedPrintRecord().getPrintDate());
        totalCountLabel.setText(mainApp.getSelectPrintRecordLabelList().size()+"");
        labelDetailList = mainApp.getSelectPrintRecordLabelList();
        initialize();
    }

    /**
     * 设置stage
     * @param dialogStage dialogStage
     */
    public void setDialogStage(Stage dialogStage) {
        this.dialogStage = dialogStage;
        this.dialogStage.getIcons().add(new Image("file:resources/images/label.png"));
    }

}
